package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Objects;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.SetMeal;
import com.sky.entity.SetMealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setMealMapper;
    @Autowired
    private SetmealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMappper;


    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void addSetMealWithDish(SetmealDTO setmealDTO) {
        // 初始化套餐及其内部菜品数据
        SetMeal setmeal = new SetMeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        List<SetMealDish> setMealDishes = setmealDTO.getSetmealDishes();

        // 插入套餐数据
        setMealMapper.insert(setmeal);

        // 设置套餐id
        setMealDishes.forEach(
                setMealDish -> setMealDish.setSetmealId(setmeal.getId())
        );

        // 批量插入套餐内部菜品数据
        setMealDishMapper.insertBatch(setMealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setMealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void multipleDelete(List<Long> ids) {
        setMealMapper.deleteByIds(ids);
        setMealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        // 获取相关数据
        SetmealVO setmealVO = setMealMapper.getById(id);
        List<SetMealDish> setmealDishes = setMealDishMapper.getSetmealDishBySetmealId(id);

        // 封装数据
        setmealVO.setSetmealDishes(setmealDishes);

        // 返回数据
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        // 更新套餐数据
        SetMeal setmeal = new SetMeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.update(setmeal);

        // 删除套餐内部菜品数据
        setMealDishMapper.deleteBySetmealId(setmealDTO.getId());

        // 批量插入套餐内部菜品数据
        List<SetMealDish> setMealDishes = setmealDTO.getSetmealDishes();
        setMealDishes.forEach(
                setMealDish -> setMealDish.setSetmealId(setmealDTO.getId())
        );
        setMealDishMapper.insertBatch(setMealDishes);
    }

    /**
     * 启售停售套餐
     * @param status
     * @param id
     */
    @Override
    public void changeSetmealStatus(Integer status, Long id) {
        if (Objects.equal(status, StatusConstant.ENABLE)) {
            List<Dish> dishes = dishMappper.getBySetmealId(id);
            if (dishes != null && !dishes.isEmpty()) {
                for (Dish dish : dishes) {
                    if (Objects.equal(dish.getStatus(), StatusConstant.DISABLE)) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        SetMeal setmeal = SetMeal.builder()
                .id(id)
                .status(status)
                .build();
        setMealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    public List<SetMeal> listByCategoryId(Long categoryId) {
        return setMealMapper.listByCategoryId(categoryId);
    }

    /**
     * 根据套餐id查询菜品
     * @param id
     * @return
     */
    public List<DishItemVO> getDishesBySetmealId(Long id) {
        return setMealDishMapper.getDishesBySetmealId(id);
    }
}
